package com.hpeu_12.zuoye1229;

import java.util.Scanner;

public class FamilyAccount {
	public static void main(String[] args) {
		//创建键盘输入对象
		Scanner scanner = new Scanner(System.in);
		//定义账户余额
		int balance = 10000;
		//用于记录收支明细
		String details = "收支\t账户金额\t收支金额\t说 明";
		//用于退出循环
		boolean flag = true;
		while(flag) {
			System.out.println("-----------------家庭收支记账软件-----------------\n1 收支明细\n2 登记收入\n3 登记支出\n4 退 出\n请选择(1-4)：");
			//接收输入的指令
			String instruct = scanner.next();
			switch (instruct) {
			case "1":
				//查看账单
				System.out.println("-----------------当前收支明细记录-----------------");
				System.out.println(details);
				break;
			case "2":
				//记录收入
				System.out.println("本次收入金额：");
				//接收键盘输入的金额
				int income = scanner.nextInt();
				//数据校验
				if (income <= 0) {
					System.out.println("对不起收入金额不能小于等于0");
				}else {
					System.out.println("本次收入说明：");
					//接收键盘输入的说明
					String incomeExplain = scanner.next();
					//更新账户余额
					balance = balance+income;
					details = details +"\n收入\t"+balance+"\t"+income+"\t"+incomeExplain;
				}

				break;
			case "3":
				//记录支出
				System.out.println("本次支出金额：");
				//接收键盘输入的金额
				int amountPaid = scanner.nextInt();
				//数据校验
				if (balance < amountPaid) {
					System.out.println("对不起支出失败，余额不足！");
				}else {
					System.out.println("本次支出说明：");
					String expenditrueExplain = scanner.next();
					//更新余额
					balance = balance - amountPaid;
					details = details +"\n支出\t"+balance+"\t"+amountPaid+"\t"+expenditrueExplain;
				}
				break;
			case "4":
				//用于退出循环
				boolean flag2 = true;
				while(flag2) {
					//退出系统
					System.out.println("确认是否退出(Y/N)");
					String exits = scanner.next();
					//equals()方法只能用于比较引用数据类型，不能用于比较基本数据类型。比较的是对象的值。
					//比较的时候把常量值写前面。变量值写后面，这样可以防止空指针异常
					//使用equalsIgnoreCase()方法比较，可以不区分大小写
					if ("Y".equalsIgnoreCase(exits)) { 
						flag = false;
						flag2 = false;
						System.out.println("退出成功！");
					}else if("N".equalsIgnoreCase(exits)) {
						flag2 = false;
						System.out.println("已取消退出");
					}else {
						System.out.println("对不起您输入的指令错误，请重新输入！");
					}
				}
				break;
			default:
				System.out.println("对不起您输入的指令错误，请重新输入！");
				break;
			}
		}
	}
}
